package com.rwaste.sms.utils;


import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Logger;

/**
 * @author raos
 * @emil 991207823@qq.com
 * @date 2020/12/25
 * 随机码/预约码/短信码生成工具类
 */
public class MateUtils {
    private static final Logger log = Logger.getLogger("MateUtils.class");
    /** 预约码码表 */
    private static final char[] CODE_TABLE= {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',
            'S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};

    /** 随机码默认位数-- 6位 */
    private static final int DEFAULT_DIGIT = 6;

    /** 默认随机码个数-- 2个 */
    private static final int DEFAULT_NUMBER = 2;

    private MateUtils() { }

    /**  默认生成单个6位预约码 */
    public static String generateOne() {
        StringBuilder strBuild = new StringBuilder();
        for (int i = 0; i <= DEFAULT_DIGIT; i++) {
            strBuild.append(CODE_TABLE[new Random().nextInt(36)]);
        }
        return strBuild.toString();
    }

    /**
     * 指定位数生成随机码，位数需大于0，否则返回默认6位随机码
     * @param digit 随机码的位数
     */
    public static String generateOne(int digit) {
        if (digit > 0) {
            StringBuilder strBuild = new StringBuilder();
            for (int i = 0; i <= digit; i++) {
                strBuild.append(CODE_TABLE[new Random().nextInt(36)]);
            }
            return strBuild.toString();
        }
        return generateOne();
    }

    /**
     * 默认生成 2个 6位随机码
     */
    public static List<String> generateList() {
        List<String> randomCodes = new ArrayList<>(DEFAULT_NUMBER);
        for (int i = 0; i < DEFAULT_NUMBER; i++) {
            randomCodes.add(generateOne());
        }
        return randomCodes;
    }

    /**
     * 指定生成随机码个数、位数生成随机码，位数需大于0，否则返回默认6位随机码，个数需大于1，否则默认2个
     * @param number 随机码生成个数
     * @param digit 随机码位数
     */
    public static List<String> generateList(int number, int digit) {
        if (number > 1) {
            List<String> randomCodes = new ArrayList<>(number);
            for (int i = 0; i < number; i++) {
                randomCodes.add(generateOne(digit));
            }
            return randomCodes;
        }
        return generateList();
    }

    /**
     * 测试入口
     */
    public static void main(String[] args) {
        log.info("循环开始" + System.currentTimeMillis());
        for (int i = 0; i < 100; i++) {
            System.out.println(generateOne());
        }
        log.info("循环结束" + System.currentTimeMillis());
    }
}
